from flask import Flask, render_template, request, jsonify
import mysql.connector

app = Flask(__name__)

# MySQL 配置
db_config = {
    'user': '你的用户名',
    'password': '你的密码',
    'host': '你服务器地址',
    'database': '数据库名字'
}

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/getTrajectory', methods=['GET'])
def get_trajectory():
    device_id = request.args.get('id')
    
    if not device_id:
        return jsonify({'error': '需要设备ID'}), 400

    conn = mysql.connector.connect(**db_config)
    cursor = conn.cursor(dictionary=True)
    
    query = """
        SELECT Longitude AS longitude, Latitude AS latitude, Time 
        FROM gps 
        WHERE Id = %s 
        ORDER BY Time ASC
    """
    cursor.execute(query, (device_id,))
    result = cursor.fetchall()

    cursor.close()
    conn.close()

    return jsonify(result)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7500)
